package com.fishsticks.model;

import java.util.ArrayList;

import com.fishsticks.R;
import com.fishsticks.view.ToDoListActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ToDoAdapter extends ArrayAdapter<ToDoItem> {

	private ArrayList<ToDoItem> toDoItems;
	private static final int CANCEL_POSITION_TAG = 111;
	private AlertDialog.Builder cancelDialog;
	//private 
	
	public ToDoAdapter(Context context, int textViewResourceId, ArrayList<ToDoItem> items) {
		super(context, textViewResourceId, items);
		toDoItems = items;
		cancelDialog = new AlertDialog.Builder(context, R.style.white_orange_dialog);
	}
	
	
	
	@Override
	public int getCount() {
		return toDoItems.size();
	}
	

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View newView = convertView;
		
		//Creates a new list item from the XML resource
        if (newView == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            newView = vi.inflate(R.layout.to_do_item_row, null);
        }
        
        final ToDoItem currentToDo = toDoItems.get(position);
        
        if (currentToDo != null) {
        		ImageView cancelIcon = (ImageView) newView.findViewById(R.id.cancel_icon);
                final TextView titleText  = (TextView) newView.findViewById(R.id.top_title_text);
                TextView bottomText = (TextView) newView.findViewById(R.id.bottom_description_text);
                CheckBox checkBox = (CheckBox) newView.findViewById(R.id.to_do_item_checkbox);
                
                
                if(cancelIcon != null) { 
                	
                	//for when you have no to-do items, you can't 
                	//  delete the one that says "no to do items"
                	if(toDoItems.size() < 1 || getCount() == 0) {
                		toDoItems.add(new ToDoItem("You have no to do's!", "Click the plus button to add a new one", null, null, null, false, -1));
                		cancelIcon.setClickable(false);
                	}
                	
                	else {
                	
	                	cancelIcon.setTag(R.id.cancel_button_id, position);
		                	
	                	cancelIcon.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) { 							
								
								final int cancelButtonTag = (Integer)v.getTag(R.id.cancel_button_id);
								
								cancelDialog.setTitle("Delete");
								cancelDialog.setMessage("Are you sure you want to delete this To-Do?");
								
								cancelDialog.setPositiveButton("Yes", new OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int arg1) {
										DatabaseManager db = new DatabaseManager(getContext());
										db.deleteToDo(toDoItems.get(cancelButtonTag));
										
										setNotifyOnChange(true);
										remove(toDoItems.get(cancelButtonTag));
										
										if(toDoItems.size() < 1 || getCount() < 1) 
								        	add(new ToDoItem("You have no to do's!", "Click the plus button to add a new one", null, null, null, false, -1));	

										db.close();
									}
								});
								
								
								cancelDialog.setNegativeButton("No", new OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int arg1) {
										dialog.dismiss();
									}
								});
								
								cancelDialog.show();
							}
						});
                	}
                }
                
                if (titleText != null) {
                	titleText.setText(currentToDo.getTitle());
                	
                	if(((String) titleText.getText()).equalsIgnoreCase("No results") || 
                		((String) titleText.getText()).equalsIgnoreCase("You have no to do's!")) {
                		
	                	if(cancelIcon != null) {
	                		cancelIcon.setClickable(false);
	                		cancelIcon.setVisibility(View.INVISIBLE);
                		}
                		
	                	if(checkBox != null) {
	                		checkBox.setClickable(false);
	                		checkBox.setVisibility(View.INVISIBLE);
	                	}
                	}
                }

                if(bottomText != null)
                	bottomText.setText(currentToDo.getDescription());
                
                if(checkBox != null) {
                	boolean checked = currentToDo.isCompleted();
                	checkBox.setChecked(checked);
                	
                	checkBox.setTag(R.id.to_do_item_checkbox, position);
                	
                	checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  						@Override
						public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
							DatabaseManager db = new DatabaseManager(getContext());
							db.markToDoAsCompleted(currentToDo, arg1);
							db.close();
							
							if(arg1)
								Toast.makeText(getContext(), titleText.getText() + " is now completed", Toast.LENGTH_SHORT).show();
							else
								Toast.makeText(getContext(), titleText.getText() + " is now incomplete", Toast.LENGTH_SHORT).show();
						}
                	});
                }
        }
        
        return newView;
	}
	
}
